#include "sample.h"
#include <QtEndian>
#include <QDebug>

sample::sample()
{
    static_assert(Q_BYTE_ORDER == Q_LITTLE_ENDIAN, "Only supports LITTLE ENDIAN");
}

uint32_t sample::getRgb() const
{
    typedef decltype(state.val) T;
    const decltype(state) s{qFromBigEndian<T>(state.val)};
    return s.rgb;
}

uint32_t sample::getBed() const
{
    const decltype(state) s {qFromBigEndian<uint32_t>(state.val)};
    return s.bed;
}

uint32_t sample::getKitchen() const
{
    const decltype(state) s{qFromBigEndian<uint32_t>(state.val)};
    return s.kitchen;
}

void sample::setRgb(uint32_t newRgb)
{
    decltype(state) s {qFromBigEndian<uint32_t>(state.val)};
    s.rgb = newRgb;
    state.val = qToBigEndian<uint32_t>(s.val);
    // Q_ASSERT(false);
}

namespace
{
constexpr double factor = 8.38190317e-8;

}
double sample::getLongitude() const
{
    double ret = qFromBigEndian<decltype(longitude)>(longitude);
    ret *= factor;
    return ret;
}

void sample::setLongitude(double newLongitude)
{
    decltype(longitude) v1 = newLongitude / factor;
    auto v2 = qToBigEndian<decltype(longitude)>(v1);
    longitude = v2;
}

void sample::print() const
{
    qDebug() << "<" << "longtitude=" << getLongitude()
             << ",bed=" << this->getBed()
             << ",kitchen=" << this->getKitchen()
             << ",rgb=" << this->getRgb();
}
